import { NzMessageService } from 'ng-zorro-antd/message';
import { CheckOutApplyList } from './../../domains/CheckOutApply.domain';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { CheckOutApplyServiceSvr } from 'src/app/service/checkOutApply.service';

@Component({
  selector: 'app-rent-management-quit',
  templateUrl: './rent-management-quit.component.html',
  styleUrls: ['./rent-management-quit.component.less']
})
export class RentManagementQuitComponent implements OnInit {

  messageTip: string = ''
  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  contractId: string = ''
  quitState: boolean = false

  quitList: any
  quitListFilter: any

  quitModal: boolean = false

  constructor(
    private checkOutApplyServiceSvr: CheckOutApplyServiceSvr,
    private activatedRoute: ActivatedRoute,
    private message: NzMessageService
  ) { }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.contractId = JSON.parse(queryParams['houseData']).ContractId
      this.customerId = JSON.parse(localStorage.getItem('customerId')!)
    })
    this.getQuitList()
    this.quitApplyStatus()
  }

  // 判断当前是否正在申请退租
  async quitApplyStatus() {
    // this.contractId = 'd4e00e894f5645c580f7512041a0b029'
    console.log(this.contractId)
    try {
      let res = await this.checkOutApplyServiceSvr. getCheckOutApplyByContractId(this.token, this.contractId)
      console.log(res)
      this.quitState = res._Items.length===0 ? true : false
      console.log(res._Items.length)
    } catch (error) {
      console.log(error)

    }
  }

  // 获取退租列表
  async getQuitList() {
    try {
      let res = await this.checkOutApplyServiceSvr.listCheckOutApplys(this.token, this.customerId, 0, '', '', 1, -1)
      this.quitList = res._Items
      this.quitListFilter = res._Items
      // console.log(this.quitList)
      this.quitApplyStatus()
    } catch (error) {
      console.log(error)
    }
  }

  // 获取不同审核状态列表
  filterQuitList_Status(state: number){
    this.quitListFilter = this.quitList.filter((item: { State: number; })=>{
      if(item.State === state) return item
      return;
    })
  }

  // 获取不同撤销状态
  filterQuitList_isDel(isDel: number) {
    this.quitListFilter = this.quitList.filter((item: { IsDeleted: number; })=>{
      if(item.IsDeleted === isDel) return item
      return;
    })
  }

  // 撤销
  async cancelQuit(id: string) {
    let res = await this.checkOutApplyServiceSvr.deleteCheckOutApply(this.token, id)
    // console.log(res)
    this.getQuitList()
    if(res===1) {
      this.message.success('退房申请撤销成功')
    } else {
      this.message.error('退房申请撤销失败')
    }
  }


  // 退租
  async quitApply() {
  //  this.contractId = 'd4e00e894f5645c580f7512041a0b029'
   try {
    let res = await this.checkOutApplyServiceSvr.saveCheckOutApply(this.token, this.customerId, this.contractId)
    this.getQuitList()
    this.message.success('退房申请已提交')
   } catch (error) {
    this.message.error('已经存在该退房申请')
   }

  }


  handleCancelQuit() {
    this.quitModal = false
  }

  async handleOkQuit() {
    this.quitModal = false
    let res = await this.checkOutApplyServiceSvr.saveCheckOutApply(this.token, this.customerId, this.contractId)
    // console.log(res)
    // if(res===1) this.message.success('退租申请已提交')

  }


  // 格式化时间
  formatTime(time: string | number | Date) {
    time = new Date(time)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    return year+'-'+month+'-'+date
  }

}
